package com.wxd.modules.function.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxd.annotation.DataFilter;
import com.wxd.modules.function.dao.RecruitNeedDao;
import com.wxd.modules.function.entity.RecruitNeedEntity;
import com.wxd.modules.function.service.RecruitNeedService;
import com.wxd.modules.sys.entity.SysDeptEntity;
import com.wxd.modules.sys.service.SysDeptService;
import com.wxd.utils.PageUtils;
import com.wxd.utils.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *招聘需求
 */

@Service("recruitNeedService")
public class RecruitNeedServiceImpl extends ServiceImpl<RecruitNeedDao, RecruitNeedEntity> implements RecruitNeedService {
    @Autowired
    private SysDeptService sysDeptService;
    @Override
    @DataFilter(subDept = true, user = false)
    public PageUtils queryPage(Map<String, Object> params) {
        String post = (String)params.get("post");
        String deptIdStr= (String) params.get("deptId");
        Long deptIdLong;
        //根据部门ID将部门下的所有人员查找出来
        List<Long> deptIdList = null;
        if(StringUtils.isNotBlank(deptIdStr))
        {

            deptIdLong= Long.valueOf(deptIdStr);
            deptIdList =sysDeptService.getSubDeptIdList(deptIdLong);
            deptIdList.add(deptIdLong);
        }
        IPage<RecruitNeedEntity> page = this.page(
                new Query<RecruitNeedEntity>().getPage(params),
                new QueryWrapper<RecruitNeedEntity>()
                        .like(StringUtils.isNotBlank(post),"post", post).in(deptIdList!=null,"dept_id",deptIdList)
        );
       /* List<RecruitNeedEntity> records = page.getRecords();

        List<RecruitNeedEntity> list = new ArrayList<RecruitNeedEntity>();
       *//* for (RecruitNeedEntity r : records) {
            Long needUserId = r.getNeedUserId();
            Long recruitUserId = r.getRecruitUserId();
            *//**//*if(null != needUserId){
                r.setNeedName(userService.getById(needUserId).getUsername());
            }*//**//*
            if(null != recruitUserId){
                r.setRecruitName(userService.getById(recruitUserId).getUsername());
            }
            list.add(r);
        }*//*
        page.setRecords(list);*/

        for(RecruitNeedEntity recruitEntity:page.getRecords())
        {
            SysDeptEntity sysdeptEntity = sysDeptService.getById(recruitEntity.getDeptId());
            recruitEntity.setDeptName(sysdeptEntity.getName());
        }
        return new PageUtils(page);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        this.removeByIds(Arrays.asList(ids));

    }

    @Override
    public void update(RecruitNeedEntity record) {
        this.updateById(record);
    }
}